<?php

/**
 * This is the model class for table "item".
 *
 * The followings are the available columns in table 'item':
 * @property string $id
 * @property string $user_id
 * @property string $name
 * @property string $description
 * @property string $quantity
 * @property string $price
 *
 * The followings are the available model relations:
 * @property User $user
 * @property User[] $comments
 */
class Item extends CActiveRecord {

	public $image;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Item the static model class
	 */
	public static function model ($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName () {
		return 'item';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules () {
		return array(
			array('name, quantity, price', 'required'),
			array('image', 'required', 'on' => 'insert'),
			array('name', 'length', 'max' => 32),
			array('description', 'length', 'max' => 256),
			array('quantity', 'numerical', 'min' => 1, 'max' => 16777215),
			array('price', 'numerical', 'min' => 0.01, 'max' => 999999.99),
			array('image', 'file', 'types' => array('jpg'), 'allowEmpty' => true),
			// The following rule is used by search().
			array('id, user_id, name, quantity, price', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations () {
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'item_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels () {
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'name' => 'Name',
			'description' => 'Description',
			'quantity' => 'Quantity',
			'price' => 'Price',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search () {
		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id, true);
		$criteria->compare('user_id', $this->user_id, true);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('quantity', $this->quantity, true);
		$criteria->compare('price', $this->price, true);

		return new CActiveDataProvider($this, array(
				'criteria' => $criteria,
			));
	}

	public function getImageFolder () {
		return Yii::getPathOfAlias('webroot.uploads');
	}

	public function getThumbnailFolder () {
		return $this->getImageFolder() . '/thumbnails';
	}

	public function getFullsizeImageFolder () {
		return $this->getImageFolder() . '/fullsize';
	}

	protected function afterSave () {
		if ($this->image) {
			$name = "/$this->id.jpg";
			$path = $this->getFullsizeImageFolder() . $name;

			$this->image->saveAs($path);
			Yii::import('ext.EImageHelper');
			$helper = new EImageHelper($path);
			$helper->limit(500, null, false)->save($path, 70)
				->limit(240, 240, true)->save($this->getThumbnailFolder() . $name, 70);
		}

		parent::afterSave();
	}

	protected function afterDelete () {
		$name = "/$this->id.jpg";
		unlink($this->getThumbnailFolder() . $name);
		unlink($this->getFullsizeImageFolder() . $name);
		parent::afterDelete();
	}

}